//
//  MyActivityViewController.m
//  Socrates
//
//  Created by shy on 15/4/12.
//  Copyright (c) 2015年 shy. All rights reserved.
//

#import "MyActivityViewController.h"

//#import "InboxViewController.h"
//#import "NotificationViewController.h"
#import "JoinedViewController.h"
#import "CreatedViewController.h"

static const CGFloat kSegmentedItemWidth = 72.f;

@interface MyActivityViewController ()

@property(strong, nonatomic) UISegmentedControl *segmentControl;
@property(weak, nonatomic) UIViewController *currentViewController;
@property(strong, nonatomic) NSArray *viewControllers;

@end

@implementation MyActivityViewController

- (void)dealloc {
    NSLog(@"%@ dealloc", self.class);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor colorWithRed:239/255. green:239/255. blue:244/255. alpha:1.];
    self.segmentControl = [[UISegmentedControl alloc] initWithItems:@[
                                                                      NSLocalizedString(@"参与", nil),
                                                                      NSLocalizedString(@"举办", nil)
                                                                      ]];
    for (int i = 0; i < self.segmentControl.numberOfSegments; i++) {
        [self.segmentControl setWidth:kSegmentedItemWidth forSegmentAtIndex:i];
    }
    self.segmentControl.selectedSegmentIndex = 0;
    [self.segmentControl addTarget:self
                            action:@selector(segmentChanged:)
                  forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = self.segmentControl;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self segmentChanged:self.segmentControl];
}

- (void)segmentChanged:(UISegmentedControl *)sender {
    [self.currentViewController.view removeFromSuperview];
    [self.currentViewController removeFromParentViewController];
    
    UIViewController *vc = [self viewControllerForSegmentIndex:sender.selectedSegmentIndex];
    vc.edgesForExtendedLayout = UIRectEdgeNone;
    CGRect frame = [UIScreen mainScreen].bounds;
    frame.size.height = CGRectGetHeight([UIScreen mainScreen].bounds) - 64;
    vc.view.frame = frame;
    [self addChildViewController:vc];
    [self.view addSubview:vc.view];
    [vc didMoveToParentViewController:self];
    
    self.currentViewController = vc;
    self.navigationItem.title = vc.title;
}

- (UIViewController *)viewControllerForSegmentIndex:(NSInteger)index {
    if (!self.viewControllers) {
        self.viewControllers = @[
                                 [[JoinedViewController alloc] init],
                                 [[CreatedViewController alloc] init],
                                 ];
    }
    return self.viewControllers[index];
}

@end
